package org.wu.framework.core.utils;

import java.lang.reflect.Proxy;

public class LazyProxyUtils {

    public static boolean isProxy(Object obj) {
        return Proxy.isProxyClass(obj.getClass());
    }

    public static boolean isJdkProxy(Object obj) {
        return isProxy(obj) && obj.getClass().getName().startsWith("$Proxy");
    }

    public static boolean hasInvocationHandler(Object proxy, Class<?> handlerType) {
        if (!isProxy(proxy)) {
            return false;
        }

        try {
            // 获取代理的InvocationHandler
            var h = Proxy.getInvocationHandler(proxy);
            // 检查InvocationHandler的类是否是handlerType的实例
            return handlerType.isInstance(h);
        } catch (IllegalArgumentException e) {
            // 非代理实例
            return false;
        }
    }
}
